﻿using System;

using FlickrApi = FlickrNet.Flickr;

using Yorrick.FlickrMetadataSynchr.Data;

namespace Yorrick.FlickrMetadataSynchr.Flickr
{
    /// <summary>
    /// Class that asynchronously fetches photo information from Flickr.
    /// </summary>
    internal sealed class AsyncMetadataUpdater: AsyncFlickrWorker<FlickrPictureMetadata, FlickrPictureMetadata>
    {
        #region Private fields
        /// <summary>
        /// The updater to use.
        /// </summary>
        private FlickrMetadataUpdater _updater;
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets whether or not to simulate updating metadata. The setter is private.
        /// </summary>
        public bool SimulateUpdatingMetadata
        {
            get;
            private set;
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="flickrApi">The Flickr API object to use.</param>
        public AsyncMetadataUpdater(FlickrApi flickrApi, bool simulateUpdatingMetadata): base(flickrApi, "UpdateFlickrMetadata")
        {
            SimulateUpdatingMetadata = simulateUpdatingMetadata;
            _updater = new FlickrMetadataUpdater(FlickrApi);
            _updater.SimulateUpdatingMetadata = SimulateUpdatingMetadata;
        }
        #endregion

        #region Protected methods
        protected override FlickrPictureMetadata GetMetadataInCaseOfError(FlickrPictureMetadata metadata)
        {
            return metadata;
        }

        protected override FlickrPictureMetadata ProcessItem(FlickrPictureMetadata metadata)
        {
            _updater.UpdatePictureMetadata(metadata);

            return metadata;
        }
        #endregion
    }
}
